package com.bdqn.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.blog.entity.Comment;
import com.bdqn.blog.service.impl.CommentServiceImpl;
import com.bdqn.blog.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author 阿桐幕
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/blog/comment")
@Api("评论接口")
public class CommentController {

    @Autowired
    private CommentServiceImpl commentService;

    //添加评论
    @ApiOperation("添加评论")
    @PostMapping("/addComment")
    public Result addComment(@RequestBody Comment comment){
        return  Result.success(commentService.save(comment));
    }

    //修改评论
    @PostMapping("/UpdataComment")
    @ApiOperation("修改评论")
    public Result saveOrUpdate(Comment comment){
        return  Result.success(commentService.saveOrUpdate(comment));
    }

    //根据评论id获取该评论的详细信息
    @PostMapping("/getCommentOfid/{id}")
    @ApiOperation("根据评论id获取该评论的详细信息")
    public Result getCommentOfid(@PathVariable Integer id){
        return  Result.success(commentService.getById(id));
    }

    //根据歌单id获取该评论的详细信息
    @ApiOperation("根据歌单id获取该评论的详细信息")
    @PostMapping("/getCommentOfSongListid/{id}")
    public Result getCommentOfSongListid(@PathVariable Integer id){
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("song_list_id",id);
        return  Result.success(commentService.list(wrapper));
    }

    //根据歌曲id获取该评论的详细信息
    @ApiOperation("根据歌曲id获取该评论的详细信息")
    @PostMapping("/getCommentOfSongid/{id}")
    public Result getCommentOfSongid(@PathVariable Integer id){
        //QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        //wrapper.eq("song_id",id);
         List<> list =  commentService.getCommentOfSongid(id);
        return  Result.success(commentService.list(wrapper));
    }

    //根据评论id删除该评
    @ApiOperation("根据评论id删除该评")
    @PostMapping("/delCommentOfid/{id}")
    public Result delCommentOfid(@PathVariable Integer id){
        return  Result.success(commentService.removeById(id));
    }


    @PostMapping("/SaveCommentUp/{id}/{up}")
    @ApiOperation("点赞数+1")
    public Result SaveCommentUp(@PathVariable(name = "id") Integer id , @PathVariable(name = "up") Integer up){
        int i = commentService.SaveCommentUp(id, up);
        if (i>0){
            return  Result.success(i);
        }else {
            return Result.fail("菜鸡程序员又在写bug呢！");
        }
    }

}

